namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganUserBuildBLL
    {
        public int Add(OrganUserBuild model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganUserBuild(");
            builder.Append("OrganName,BuildCode,Count_Allow");
            builder.Append(") values (");
            builder.Append("@OrganName,@BuildCode,@Count_Allow");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@BuildCode", model.BuildCode), new SqlParameter("@Count_Allow", model.Count_Allow) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int CountID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganUserBuild ");
            builder.Append(" where CountID=@CountID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CountIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganUserBuild ");
            builder.Append(" where ID in (" + CountIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<OrganUserBuild> GetList(int OrganID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM OrganUserBuild ");
            builder.Append("  WHERE OrganName= ").Append(OrganID);
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            OrganUserBuild item = null;
            List<OrganUserBuild> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<OrganUserBuild>();
                foreach (DataRow row in table.Rows)
                {
                    item = new OrganUserBuild();
                    if (row["CountID"].ToString() != "")
                    {
                        item.CountID = int.Parse(row["CountID"].ToString());
                    }
                    item.OrganName = row["OrganName"].ToString();
                    item.BuildCode = row["BuildCode"].ToString();
                    if (row["Count_Allow"].ToString() != "")
                    {
                        item.Count_Allow = int.Parse(row["Count_Allow"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganUserBuild GetModel(int CountID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CountID, OrganName, BuildCode, Count_Allow  ");
            builder.Append("  from OrganUserBuild ");
            builder.Append(" where CountID=@CountID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountID;
            OrganUserBuild build = new OrganUserBuild();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["CountID"].ToString() != "")
                {
                    build.CountID = int.Parse(row["CountID"].ToString());
                }
                build.OrganName = row["OrganName"].ToString();
                build.BuildCode = row["BuildCode"].ToString();
                if (row["Count_Allow"].ToString() != "")
                {
                    build.Count_Allow = int.Parse(row["Count_Allow"].ToString());
                }
                return build;
            }
            return null;
        }

        public bool Update(OrganUserBuild model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganUserBuild set ");
            builder.Append(" OrganName = @OrganName , ");
            builder.Append(" BuildCode = @BuildCode , ");
            builder.Append(" Count_Allow = @Count_Allow  ");
            builder.Append(" where CountID=@CountID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountID", model.CountID), new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@BuildCode", model.BuildCode), new SqlParameter("@Count_Allow", model.Count_Allow) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

